package com.abigdreamer.im.server.cmd.commandAction;

import com.abigdreamer.im.server.cmd.resouse.CMDConfig;
import com.abigdreamer.im.server.cmd.resouse.CommandBean;
import com.abigdreamer.im.server.cmd.resouse.ConfigFactory;

public class ActionFactory {

	private static ActionFactory instance;
	private CMDConfig config;
	
	public static ActionFactory getInstance() {
		
		if(null==instance){
			instance=new ActionFactory();
		}
		return instance;
	}

	private ActionFactory(){
		config=ConfigFactory.getInstance().getCMDConfig();
	}
	
	/**
	 * 
	 * @return
	 */
	public CommandAction getCommandActionByID(String  commandName){
		CommandAction action=null;
		CommandBean commandBean=config.getCommandBeanByID(commandName);
		try {
			String commandClass=commandBean.getClassPath();
			action=(CommandAction)Class.forName(commandClass).newInstance();
		} catch (Exception e) {
			e.printStackTrace();
		} 
		action.setNextAction(commandBean.getNextID());
		action.setOperateParam(commandBean.getOperateParam());
		return action;
	}
	
	public CommandAction getCommandActionByName(String actionName) {
		CommandAction action=null;
		CommandBean commandBean=config.getCommandBeanByName(actionName);
		try {
			String commandClass=commandBean.getClassPath();
			action=(CommandAction)Class.forName(commandClass).newInstance();
		} catch (Exception e) {
			e.printStackTrace();
		} 
		action.setNextAction(commandBean.getNextID());
		action.setOperateParam(commandBean.getOperateParam());
		return action;
	}
	
	
	public CMDConfig getConfig() {
		return config;
	}

	public void setConfig(CMDConfig config) {
		this.config = config;
	}



}
